

//
//  SQliteManager.m
//  kuangjia
//
//  Created by kakao on 16/1/26.
//  Copyright © 2016年 ZhangShuai. All rights reserved.
//
#import "FMDB.h"
static NSString * const dbname = @"status.db";

#import "SQliteManager.h"

@implementation SQliteManager

static SQliteManager *_instance=nil;

+(instancetype)shareInstance{

    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
       
        _instance=[[self alloc] init];
    });

    return _instance;
}


-(instancetype)init{

    if (self==[super init]) {
        [self createTable];
    }
    return self;
}


-(void)createTable{
  NSString *docsdir = [NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path=[docsdir stringByAppendingPathComponent:dbname];
    
    NSLog(@"%@",path);
    self.queue= [[FMDatabaseQueue alloc] initWithPath:path];
    
    NSString  *sqlPath=[[NSBundle mainBundle] pathForResource:@"table.sql" ofType:nil];
    
    NSString *sqlStr=[[NSString alloc]initWithContentsOfFile:sqlPath encoding:NSUTF8StringEncoding error:nil];
    
    
    [self.queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        
        if ([db executeStatements:sqlStr]) {
            
            NSLog(@"创表成功");
        }else{
        
            NSLog(@"创表失败");
        }
        
    }];
    
}



@end
